/******************************************************************************
 * Copyright (C) 2010 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.threadlocalfilter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

import com.code.common.systeminit.EnviromentInfo;
import com.code.common.systeminit.ServerConstant;

/**
 * 通用清除线程变量过滤器
 * 
 * @author 郑重
 * @since 1.0
 * @version 2010-11-5 郑重
 */
public class ThreadLocalClearFilter extends OncePerRequestFilter {
    
    /** 日志对象 */
    private final static Logger LOGGER = LoggerFactory.getLogger(ThreadLocalClearFilter.class);
    
    /**
     * 线程变量配置
     */
    private ThreadLocalClearFilterConfig threadLocalClearFilterConfig;
    
    /**
     * @throws ServletException ServletException
     * @see org.springframework.web.filter.GenericFilterBean#initFilterBean()
     */
    @Override
    protected void initFilterBean() throws ServletException {
        super.initFilterBean();
        threadLocalClearFilterConfig = EnviromentInfo.getWebApplicationContext().getBean(ThreadLocalClearFilterConfig.class);
    }
    
    /**
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param filterChain FilterChain
     * @throws ServletException ServletException
     * @throws IOException IOException
     * @see org.springframework.web.filter.OncePerRequestFilter#doFilterInternal(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException,
        IOException {
        long lBegin = System.currentTimeMillis();
        boolean bIsException = false;
        LOGGER.debug("清除线程变量过滤器清除请求路径中的变量:{}", request.getRequestURL());
        if (!request.getRequestURI().endsWith(ServerConstant.EXCLUDE_FILTER_PATH)) {
            for (String strClassName : threadLocalClearFilterConfig.getFilterList()) {
                ThreadLocalClearInterface objThreadLocalClearInterface;
                try {
                    objThreadLocalClearInterface = (ThreadLocalClearInterface) Class.forName(strClassName).newInstance();
                    objThreadLocalClearInterface.clearThreadLocal();
                } catch (Exception e) {
                    bIsException = true;
                    LOGGER.error(strClassName + "执行错误", e);
                }
            }
        }
        if (bIsException) {
            throw new ServletException("ThreadLocalClearFilter清楚线程变量错误");
        }
        long lEnd = System.currentTimeMillis();
        LOGGER.debug("清除线程变量共耗费时间：[{}ms]", lEnd - lBegin);
        filterChain.doFilter(request, response);
        lEnd = System.currentTimeMillis();
        LOGGER.debug("本次请求共耗费时间：[{}ms]", lEnd - lBegin);
    }
}